# Maintainer: @naveen521kk on Github, Naveen M K <naveen521kk@gmail.com>

_realname=delta
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=0.16.5
pkgrel=1
pkgdesc='A syntax-highlighting pager for git and diff output (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://github.com/dandavison/delta"
license=('spdx:MIT')
depends=('git')
makedepends=("${MINGW_PACKAGE_PREFIX}-rust")
source=("https://github.com/dandavison/delta/archive/refs/tags/$pkgver.tar.gz"
        "ntapi-link-ntdll-arm64.patch")
sha256sums=('00d4740e9da4f543f34a2a0503615f8190d307d1180dfb753b6911aa6940197f'
            'c17674896bd2cd559fdf48812200096e5dcd93160874c05a46f21585d8202f16')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  cargo vendor \
    --locked \
    --versioned-dirs
  mkdir -p .cargo
  cat >> .cargo/config.toml <<END

[source.crates-io]
replace-with = "vendored-sources"

[source.vendored-sources]
directory = "vendor"
END

  patch -d vendor/ntapi-0.4.0 -Np1 -i "${srcdir}/ntapi-link-ntdll-arm64.patch"
}

build() {
  cd "${srcdir}"
  cp -r ${_realname}-${pkgver} build-${MSYSTEM}
  cd build-${MSYSTEM}
  export WINAPI_NO_BUNDLED_LIBRARIES=1
  ${MINGW_PREFIX}/bin/cargo build \
	--release \
	--frozen
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"
  ${MINGW_PREFIX}/bin/cargo test \
	--release \
	--frozen
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  ${MINGW_PREFIX}/bin/cargo install \
	--frozen \
	--offline \
	--no-track \
	--path . \
	--root ${pkgdir}${MINGW_PREFIX}

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
  install -Dm644 etc/completion/completion.bash "${pkgdir}${MINGW_PREFIX}/share/bash-completion/completions/$_realname"
  install -Dm644 etc/completion/completion.zsh  "${pkgdir}${MINGW_PREFIX}/share/zsh/site-functions/_$_realname"
}
